<template>
  <el-upload
    ref="uploadFile"
    list-type="picture"
    :action="upload_file_url"
    :headers="headers"
    multiple
    :limit="5"
    :before-upload="handleBeforeUpload"
    :on-remove="handleRemove"
    :on-success="handleUploadSuccess"
    :on-exceed="handleExceed"
  >
    <el-button size="small" type="text">点击上传</el-button>
    <div slot="tip" class="el-upload__tip">
      （仅支持jpg、png、gif、pdf格式上传）
    </div>
  </el-upload>
</template>

<script>
// import { getToken } from "@/utils/auth";
export default {
  data() {
    return {
      upload_file_url:
        process.env.VUE_APP_BASE_URL + "/api/v1/uploadFile/pictureUpload",
      headers: {
        Authorization: "6a77998089b8467d83d2094a8011b02d",
        Token: "Bearer " + '',
      },
    };
  },
  methods: {
    // 上传文件种类
    handleBeforeUpload(file) {
      console.log(file, "3333333shagchi");
      let fileName = file.name;
      let fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
      let fileTypJuge =
        fileType === "png" ||
        fileType === "jpg" ||
        fileType === "gif" ||
        fileType === "pdf" ||
        fileType === "PNG" ||
        fileType === "JPG" ||
        fileType === "GIF" ||
        fileType === "PDF";
      let fileSize = Number(file.size / 1024 / 1024);
      if (!fileTypJuge) {
        this.$message.error("上传文件仅支持jpg、png、gif、pdf格式");
      }
      if (fileSize > 5) {
        this.$message({
          message: "文件大小不能超过5M！",
          type: "warning",
        });
        return false;
      }
    },
    // 上传文件成功
    handleUploadSuccess(response, file, fileList) {
      console.log(response, "222222");
      let fileBox = {};
      if (response.code == 200) {
        this.$message.success("上传成功");
        this.$emit("childEvent", response.result);
      }
    },
    // 文件个数超出
    handleExceed() {
      this.$message.error(`上传文件数量不能超过5个!`);
    },
    //上传文件删除
    handleRemove(file, fileList) {
      console.log(file.response.result.absoluteUrl, fileList);
      this.add_form.relationPictureIds.forEach((item, index) => {
        if (file.response.result.absoluteUrl == item.url) {
          let params = {
            filePath: item.url,
          };
        }
      });
    },
  },
};
</script>

<style scoped>
</style>
